﻿namespace LxBasic
{
    public class CheckCreator
    {
        public static bool ArrayStrict<T>(ref T[]? array, int count)
        {
            if (array == null || array.Length != count)
            {
                array = new T[count];
                return true;
            }
            return false;
        }
        public static bool ArrayEnough<T>(ref T[]? array, int count)
        {
            if (array == null || array.Length < count)
            {
                array = new T[count];
                return true;
            }
            return false;
        }
        public static bool Instance<T>(ref T? refValue) where T : new()
        {
            if (refValue == null)
            {
                refValue = new T();
                return true;
            }
            return false;
        }
        public static bool Instance<TSpecific, TBase>(ref TBase? valueNow, out TSpecific newValue) where TSpecific : class, TBase, new()
        {
            if (valueNow == null || valueNow.GetType() != typeof(TSpecific))
            {
                valueNow = newValue = new TSpecific();
                return true;
            }
            else
            {
                newValue = valueNow as TSpecific;
                return false;
            }
        }

        //public static bool InstanceReserve<TSpecific, TBase>(ref TBase valueNow, out TSpecific newValue) where TSpecific : class, TBase, IReuse, new()
        //{
        //    if (valueNow == null)
        //    {
        //        valueNow = newValue = new TSpecific();
        //        return true;
        //    }
        //    else if (valueNow.GetType() != typeof(TSpecific))
        //    {
        //        newValue = new TSpecific();
        //        newValue.Reuse(valueNow);
        //        valueNow = newValue;
        //        return true;
        //    }
        //    else
        //    {
        //        newValue = valueNow as TSpecific;
        //        return false;
        //    }
        //}
        //public interface IReuse
        //{
        //    void Reuse(object oldObject);
        //}
    }
    //public interface IReuse
    //{
    //    void Reuse(object oldObject);
    //}
}

